import queue
import threading
import copy

shared_resource_lock_pool = threading.Lock()  ## 锁


class KThread(threading.Thread):
    def __init__(self, threadID, name, counter,
                 classFunc=None,
                 func=None,
                 args=None,
                 needpool=False,
                 isHadArgs=False,
                 debugMode=False):

        self.sthreadpool = None
        self.taskQueue = None
        self.sthreadcount = None
        self.sclassFunc = classFunc
        self.sneedpool = needpool
        #super().__init__(self)
        if debugMode:
            print("a KThread class has been built")
        # print("a ")
        threading.Thread.__init__(self)
        self.sthreadID = threadID
        self.sname = name
        self.scounter = counter

        self.sfunc = func
        self.sargs = args
        self.sIsHadArgs = isHadArgs

    def setQueue(self, que: queue, threadcount=None):
        self.taskQueue = que
        # self.sthreadcount = threadcount
    def setfunc(self, func, args, isHadArgs):
        self.sfunc = func
        self.sargs = args
        self.sIsHadArgs = isHadArgs

    def setParentPool(self, threadpool):
        self.sthreadpool = threadpool

    def run(self):
        if not self.sIsHadArgs:
            self.sfunc()
        else:
            self.sfunc(self.sargs)
        # print(self.sneedpool)
        print(self.sthreadID, self.sname, "taskDone")
        if self.sneedpool:
            # print("11111111111111111111111111111111111111111111111111111")
            shared_resource_lock_pool.acquire()

            # self.sthreadcount += 1
            # print( self.sthreadpool)
            # print(self.sthreadcount,11111111111111111111111213123123)
            # print(hex(id(self.sthreadpool)))
            self.sthreadpool.taskDonePool()

            # self.taskQueue.task_done()
            # self.sthreadpool.startPool()
            shared_resource_lock_pool.release()


class KThreadPool:
    smaxThreads = 20
    nowThreadCount = 20

    # taskQueue=None
    def __init__(self, maxthreads: int, debugmode=False):
        # print(hex(id(self)))
        # threading.Thread.__init__(self)
        # self.nowThreadCount = 20
        if debugmode:
            print("a KThreadPool class has been built")

        self.taskQueue = queue.Queue()
        self.setSMaxThreads(maxthreads)
        #self.taskQueue.maxsize

    # def submit(self,func,args):
    #     self.taskQueue.put((func,args))
    def setSMaxThreads(self, i):
        self.smaxThreads = i
        self.nowThreadCount = (self.nowThreadCount - self.smaxThreads) + self.smaxThreads

    def taskDonePool(self):
        self.nowThreadCount += 1
        self.taskQueue.task_done()
        self.startPool()

    def pushThreadsAndStart(self, thread: KThread):
        # self.taskQueue.
        # thread.setQueue(self.taskQueue)  #浅拷贝
        thread.setParentPool(self)
        # print(self.taskQueue)
        self.taskQueue.put(thread)
        self.startPool()

    def pushThreadsAnd(self, thread: KThread):
        # self.taskQueue.
        # thread.setQueue(self.taskQueue)  #浅拷贝
        thread.setParentPool(self)
        self.taskQueue.put(thread)

        # self.startPool()

    def startPool(self):
        print(self.nowThreadCount)
        while self.nowThreadCount:
            if self.taskQueue.empty():
                break
            self.nowThreadCount -= 1
            nowThreadToRun = self.taskQueue.get()
            nowThreadToRun.start()

            # self.taskQueue.task_done()
